﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StuManModels;
using StuManDAL;

namespace StudentManageWinForm
{
    public partial class FrmStudentManage : Form
    {
        public FrmStudentManage()
        {
            InitializeComponent();
            Init();
        }
        private ClassService _classService = null;
        private StudentService _service = null;
        private BindingSource _source = null;
        private void Init()
        {
            _classService = new ClassService();
            cmbClass.DataSource = _classService.GetAllClass(out _);
            cmbClass.DisplayMember = "ClassName";
            cmbClass.ValueMember = "ClassId";
            cmbClass.SelectedIndex = -1;
            _service = new StudentService();
            _source = new BindingSource();
            dgvStudentInfo.Columns["ColumnClassName"].Visible = false;
            dgvStudentInfo.AutoGenerateColumns = false;
            dgvStudentInfo.DataSource = _source;
        }
        private void QueryByStuClass()
        {
            dgvStudentInfo.Columns["ColumnClassName"].Visible = false;
            _source.DataSource= _service.QueryStudentsByClass(Convert.ToInt32(cmbClass.SelectedValue), out string info);
        }
        private void QueryByStuId()
        {
            string stuId = txtbStuId.Text.Trim();
            if (!StuManDAL.Common.DataValidate.Instance.IsPositiveInteger(stuId))
            {
                MessageBox.Show("学号格式错误");
                return;
            }
            dgvStudentInfo.Columns["ColumnClassName"].Visible = true;
            _source.DataSource = _service.QueryStudentByStuId(Convert.ToInt32(stuId), out _);
        }
        private void DeleteStudent()
        {
            if (_source.Current==null)
            {
                lbInfo.Text = "请选择要删除的学员";
                return;
            }
            StudentExt studentExt = _source.Current as StudentExt;
            DialogResult result = MessageBox.Show($"确认删除学员[{studentExt.StuName}]么？\n 数据将被彻底删除！", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result!=DialogResult.Yes)
            {
                return;
            }
            _service.DeleteStudent(studentExt.StuId, out string info);
            lbInfo.Text = info;
            _source.RemoveCurrent();
        }
        private void ModifyStudent()
        {
            if (_source.Current==null)
            {
                lbInfo.Text = "请选择要修改的学员";
                return;
            }
            StudentExt studentExt = _source.Current as StudentExt;
            DialogResult result = MessageBox.Show($"确认修改学员[{studentExt.StuName}]么？！", "修改确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result != DialogResult.Yes)
            {
                return;
            }
            _service.ModifyStudent(studentExt, out string info);
            lbInfo.Text = info;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryByStuClass();
        }

        private void btnQueryByStuId_Click(object sender, EventArgs e)
        {
            QueryByStuId();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DeleteStudent();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            ModifyStudent();
        }
    }
}
